package com.yuwei.module_repair


import android.app.Activity
import android.content.Intent
import android.os.Build
import android.view.ContextThemeWrapper
import android.view.Gravity
import android.view.View
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.entity.AddRepairEntity
import com.bw.lib_common.view.BaseMvvmActivity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import com.yuwei.module_repair.databinding.ActivityRepairBinding
import com.yuwei.module_repair.viewmodel.RepairViewModel

/**
 * 智慧园区-06-物业报修V1.1-添加报修-20250507-崔志彪
 */
@Route(path = RouterPath.MODULE_ADD_REPAIR_ACTIVITY)
class RepairActivity : BaseMvvmActivity<ActivityRepairBinding>() {
    private val viewModel: RepairViewModel by viewModels()

    @RequiresApi(Build.VERSION_CODES.M)
    override fun initViews() {
        super.initViews()
        binding.repairBack.setOnClickListener {
            finish()
        }
        binding.repairSubmit.setOnClickListener {
            val repairEntityItem= AddRepairEntity(
                id = 0,
                userId = 443,
                orderNumber = binding.repairNumber.text.toString(),
                repairName = binding.repairName.text.toString(),
                contactMethod = 18300624657,
                repairLocation = 1,
                processingStatus = "待处理",
                repairTime = System.currentTimeMillis().toString(),
                faultDescription = binding.repairDesc.text.toString(),
                faultPhotos = "http://example.com/photo1.jpg,http://example.com/photo2.jpg"
            )
            viewModel.addRepair(repairEntityItem)
        }
        binding.addImg2.setOnClickListener {
            PictureSelector.create(this, 512).selectPicture()
        }
        val wrapper =
            ContextThemeWrapper(this, com.bw.lib_common.R.style.CustomPopupMenu)
        val popupMenu = PopupMenu(wrapper, binding.repairLocationArrow)
        popupMenu.menuInflater.inflate(R.menu.location_menu, popupMenu.menu)
        popupMenu.gravity = Gravity.BOTTOM
        binding.repairLocationArrow.setOnClickListener {
            popupMenu.show()
        }
        popupMenu.setOnMenuItemClickListener {
            binding.repairLocation.setText(it.title.toString())
            false
        }
    }

    override fun observeDataChange() {
        viewModel.addRepairSuccess.observe(this){
            ToastUtils.showLong("提交成功")
            finish()
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 512 && resultCode == Activity.RESULT_OK) {
            val extras = data?.extras
            val pictureBean = extras?.get(PictureSelector.PICTURE_RESULT) as? PictureBean
            pictureBean?.path?.let { path ->
                binding.addImg.visibility= View.VISIBLE
                Glide.with(this).load(path).into(binding.addImg)
            }
        }
    }

    override fun getLayoutId(): Int =R.layout.activity_repair
}